package com.siyuanren.gcocp.web;

import com.siyuanren.gcocp.common.config.PublicConfig;
import com.siyuanren.gcocp.common.util.CommonUtils;
import com.siyuanren.gcocp.common.util.DateUtils;
import com.siyuanren.gcocp.common.util.UUIDUitl;
import com.siyuanren.gcocp.common.web.bean.Result;
import com.siyuanren.gcocp.consts.GcocpConsts;
import com.siyuanren.gcocp.exception.GcocpException;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * Created by   guwr
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.web.FileController
 * Date         2017/4/19
 * Time         13:12
 * Description  文件
 */
@Controller
@RequestMapping("file")
public class FileController {

    private static Logger logger = LoggerFactory.getLogger(FileController.class);

    //上传文件会自动绑定到MultipartFile中
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @ResponseBody
    public Result upload(HttpServletRequest request, @RequestParam Map<String,String> map, @RequestParam("file") MultipartFile file) throws Exception {
        Result result = new Result();
        String fileName = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(fileName);
        validateUploadFile(file, extension);
        String path = "config/" + DateUtils.getReqDate();
        new File (PublicConfig.USER_DATA_PATH + path).mkdirs();
        File destFile = new File( PublicConfig.USER_DATA_PATH + path, fileName);
//        File destFile = new File("D:\\tomcat-upload-tmp\\" + GcocpConsts.RESOURCE_URL_IMG, newFileName);
        if (!destFile.exists()) {
            //完成文件上传
            file.transferTo(destFile);
        } else {
            result.setCode(1);
            result.setMsg("error: file has exist");
        }
        map.put("fileName", path + GcocpConsts.FILE_SEPARATOR + fileName);
        result.setData(map);
        return result;
    }

    /**
     * 文件格式校验
     */
    public void validateUploadFile(MultipartFile file, String extension) {
        if (file == null) {
            // 没有上传文件
            logger.info("validateUploadFile file is null");
            throw GcocpException.UPLOAD_FILE_RANGE_ERROR.print();
        }
        if (file.getSize() > GcocpConsts.UPLOAD_IMG_MAX_FILE_SIZE) {
            // 上传的文件文件不能大于10M
            logger.info("validateUploadFile file size error");
            throw GcocpException.UPLOAD_FILE_RANGE_ERROR.print();
        }
        if (!"jpg".equals(extension) && !"gif".equals(extension) && !"jpeg".equals(extension) && !"png".equals(extension)) {
            logger.info("validateUploadFile extension error");
            throw GcocpException.UPLOAD_FILE_RANGE_ERROR.print();
        }
    }
}
